//
//  WBBaseViewController.swift
//  传智微博
//
//  Created by FYXJ（6） on 2018/8/27.
//  Copyright © 2018年 FYXJ（6）. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {

    var userLogon = true
    var visitorInfoDic: [String:String]?
    var tableView: UITableView?
    var refreshControl: UIRefreshControl?
    
    var isPullup = false
    
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: UIScreen.cz_screenWidth(), height: 49))
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        setupUI()
        loadData()
    }
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    
    @objc func loadData() {
        refreshControl?.endRefreshing()
    }
    override func viewDidLayoutSubviews() {
     navigationBar.frame = CGRect(x: 0, y: 20, width: UIScreen.cz_screenWidth(), height: 64)
    }
//    override func layoutSubviews() {
//        super.layoutSubviews();
//        //注意导航栏及状态栏高度适配
//        frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
//        for view in subviews {
//            let viewbg = subviews[0];//_UIBarBackground
//            let viewcon = subviews[2];//_UINavigationBarContentView
//            print(view);
//            if(view == viewbg){
//                view.frame = bounds;
//            }else if(view == viewcon){
//                var frame = view.frame;
//                frame.origin.y = 20;
//                frame.size.height = self.bounds.size.height - frame.origin.y;
//                view.frame = frame;
//            }
//        }
//    }
}

extension WBBaseViewController {
    
    @objc private func login() {
        
    }
    
    @objc private func register() {
        
    }
}
// MARK: -设置界面
extension WBBaseViewController {
   
   private func setupUI() {
        view.backgroundColor = UIColor.white

        setupNavigationBar()
        userLogon ? setupTableView() : setupVisitorView()
    }
    
    private func setupVisitorView() {
        
        let visitorView = WBVisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navigationBar)
        visitorView.visitorInfo = visitorInfoDic
        
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
    }
    @objc func setupTableView()  {
    tableView = UITableView(frame: view.bounds, style: .plain)
    view.insertSubview(tableView!, belowSubview: navigationBar)
    
    //设置数据源&代理
    tableView?.dataSource = self
    tableView?.delegate = self
    
    tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
    
    refreshControl = UIRefreshControl()
    tableView?.addSubview(refreshControl!)
    
    refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    private func setupNavigationBar() {
        view.addSubview(navigationBar)
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationBar.items = [navItem]
        navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.darkGray]
        navigationBar.tintColor = UIColor.orange
    }
}

extension WBBaseViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        
        if row<0 || section<0 {
            return
        }
        
        let rowCount = tableView.numberOfRows(inSection: section)
        
        if row == (rowCount-1) && !isPullup {
            print("上拉刷新")
            isPullup = true
            loadData()
        }
    }
}
